package com.nd.android.jsondemo;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class U {
	public static boolean debugFlag = true;
	public static String nLine = System.getProperty( "line.separator" );
	
	public static String getDisplayDateStr(String dateStr){
		//dateStr = "Thu Apr 30 01:33:41 +0000 2009";
		Date dd = new Date(dateStr);
		SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); 
		String str = "";
		try{
			str = myFmt.format(dd);			
		}catch(Exception e){
			e.printStackTrace();
		}
		dout(str);
		return str;
	}
	
	public static String getDateStr(String dateStr){
		Date date = str2Date(dateStr);
		return getHowLongStr(date);		
	}
	
	/**输入"Thu Apr 30 01:33:41 +0000 2009" 返回Date*/
	public static Date str2Date(String dateStr){
		return new Date(dateStr);
	}
	
	/**传入一个Date，判断是多久前，返回示例"4小时前"*/
	public static String getHowLongStr(Date date){
		String rs = "";
		Long i = date.getTime();
		Date now = new Date();
		Long j = now.getTime();
		dout(j - i);
		long day = 1000 * 60 * 60 * 24;
		long hour = 1000 * 60 * 60;
		long min = 1000 * 60;
		long sec = 1000;
		if (((j-i)/day)>0)
			rs = ((j-i)/day)+"天前";
		else if (((j-i)/hour)>0)
			rs = ((j-i)/hour)+"小时前";
		else if (((j-i)/min)>0)
			rs = ((j-i)/min)+"分钟前";
		else if (((j-i)/sec)>0)
			rs = ((j-i)/sec)+"秒前";
		return rs;
	}
	
	/**根据一个网络上的图片url，返回一个Bitmap*/
	public static Bitmap returnBitMap(String url) {
		URL myFileUrl = null;
		Bitmap bitmap = null;
		try {
			myFileUrl = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream is = conn.getInputStream();
			bitmap = BitmapFactory.decodeStream(is);
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}
		
	public static void dout(Object obj){
		if(debugFlag)
		Log.d("[dout]","obj>>>>>>>>>>>>>"+obj.getClass().getName()+">>"+obj.toString());
	}
	
	public  static void dout(String str){
		if(debugFlag)
		Log.d("[dout]","str>>>>>>>>>>>>>"+str);
	}
	
	public  static void dout(String[] str){
		if(debugFlag){
		for(int i=0; i<str.length; i++)
			Log.d("[dout]","str["+i+"]>>>>>>>>>>>>>"+str[i]);
		}
	}		
	
	public static String str2Abc(String str){
		String rs = "";
		if (str.equals("1"))
			rs = "A";
		if (str.equals("2"))
			rs = "B";
		if (str.equals("3"))
			rs = "C";
		if (str.equals("4"))
			rs = "D";
		return rs;
	}
	
	/**把1234转换成ABCD*/
	public static String int2Abc(int num){
		String rs = "";
		if (num==1)
			rs = "A";
		if (num==2)
			rs = "B";
		if (num==3)
			rs = "C";
		if (num==4)
			rs = "D";
		return rs;
	}
	
	/**
	 * 去除字符串中的空格,回车,换行符,制表符
	 * */
	public static String rmStrBlank(String str){
		str = str.trim();
		return str.replaceAll("\\s*|\t|\r|\n","");
	}
}
